package com.example.lasttrain;

import com.example.lasttrain.common.DatabaseUtils;
import com.example.lasttrain.common.Defines;
import com.example.lasttrain.common.LeftTimeCalculate;
import com.example.lasttrain.common.TimeData;
import com.example.lasttrain.common.TrainEntry;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout;

public class DetailActivity extends Activity {
	
	TrainEntry trainInfo;
	
	TextView textName, textStart, textEnd, textTime, textAlarm;
	LinearLayout btnView, btnConfirm; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_detail);
        
        textName = (TextView)findViewById(R.id.content_name);
        textStart = (TextView)findViewById(R.id.content_start);
        textEnd = (TextView)findViewById(R.id.content_end);
        textTime = (TextView)findViewById(R.id.content_time);
        textAlarm = (TextView)findViewById(R.id.content_alarm);
        btnView = (LinearLayout)findViewById(R.id.view_buttons);
        btnConfirm = (LinearLayout)findViewById(R.id.confirm_buttons);
        
        // IDを渡されて呼ばれた場合は、紐づく情報を取得する
        if(getIntent().hasExtra(Defines.INTENT_DB_NUMBER)){ //get the station information from DB, if the intent has an available DB id...
        	
        	int dbId = getIntent().getIntExtra(Defines.INTENT_DB_NUMBER, 0);
        	trainInfo = DatabaseUtils.getContents(this, dbId);
        	
        	btnView.setVisibility(View.VISIBLE);
        	btnConfirm.setVisibility(View.GONE);
        }
        else{
        	trainInfo = TrainEntry.getNewEntry();
        	
        	btnConfirm.setVisibility(View.VISIBLE);
        	btnView.setVisibility(View.GONE);
        }
        
        if(trainInfo == null){
        	finish();
        	return;
        }
        
        textName.setText(trainInfo.getName());
        textStart.setText(trainInfo.getStartStation());
        textEnd.setText(trainInfo.getEndStation());
        // あと何分を算出してその時間をセット
        TimeData tmpTime = trainInfo.getTrainTime();
        LeftTimeCalculate leftTimeCal = new LeftTimeCalculate();
        textTime.setText(leftTimeCal.calAlarm(tmpTime));
        // 何分前にお知らせをセット
        String[] alarmString = getResources().getStringArray(R.array.spinner_time);
        textAlarm.setText(alarmString[trainInfo.getAlarmtime()]);

    }
    
	private void moveToCreateActivityToEdit(int id){
		Intent intent = new Intent(this, CreateEditActivity.class);
		intent.putExtra(Defines.INTENT_DB_NUMBER, id);
		startActivityForResult(intent, 0);
	}
	
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode == RESULT_OK)
			finish();
	}
    
    private void saveTrainInformation(){
    	//save data to DB at here...
    	DatabaseUtils.upRecord(this, trainInfo);
    	
    	// もしアラーム設定中のものだったら、アラーム更新をかける(上手くかからないので、時期リリースへ持越し)
    	if(trainInfo.getAlarmFlg() == 1){	
	    	AlarmManageStart alarm = new AlarmManageStart(this);
	    	alarm.stopAlarm(trainInfo.getDbId());
	    	alarm.startAlarm(trainInfo.getTrainTime().getHour(), trainInfo.getTrainTime().getMinute(),
	    		trainInfo.getAlarmtime(), trainInfo.getDbId());
	    }
    }
    
    private void confirmDelete(){
    	AlertDialog alert = new AlertDialog(this){};
    	alert.setButton(DialogInterface.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// Auto-generated method stub
				deleteRecord();
			}
		});
    	alert.setButton(DialogInterface.BUTTON_NEGATIVE, "cancel", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// Auto-generated method stub
				
			}
		});
    	alert.setTitle(R.string.detail_delete_confirm);
    	
    	alert.show();
    }
    
    public void deleteRecord(){
    	DatabaseUtils.deleteContents(this, trainInfo.getDbId());
    	Toast.makeText(this, R.string.detail_delete_message, Toast.LENGTH_SHORT).show();
		finish();
    }
    
    public void mOnClick(View v){
    	switch(v.getId()){
		case R.id.button_ok:
				saveTrainInformation();
				Toast.makeText(this, R.string.notice_success, Toast.LENGTH_SHORT).show();
				setResult(RESULT_OK);
				finish();
			break;
			
		case R.id.button_edit:
			moveToCreateActivityToEdit(trainInfo.getDbId());
			break;
			
		case R.id.button_delete:
			
			//DatabaseUtils.deleteContents(this, trainInfo.getDbId());
			//finish();
			confirmDelete();
			break;
			
		case R.id.button_cancel:
		case R.id.button_back:
			finish();
			break;
		}
    	
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_detail, menu);
        return true;
    }
}
